import userService from 'SERVICE/userService'
import Immutable, { fromJS } from 'immutable';
// ================================
// Action Type
// ================================
export const ADD_USER = 'ADD_USER';
export const UPDATE_USER = 'UPDATE_USER';
export const DEL_USER = 'DEL_USER';
export const LIST_USER = 'LIST_USER';

// ================================
// Action Creator
// ================================
export function addUser(item) {
  return { type: ADD_USER, payload: item }
}

export function updateUser(item) {
  return { type: UPDATE_USER, payload: item }
}

export function delUser(index) {
  return { type: DEL_USER, payload: index }
}

const listUser = queryBody => dispatch =>
  userService
    .fetch(queryBody)
    .then(userData => dispatch({
      type: LIST_USER,
      payload: fromJS(userData)
    }))

export default {
  listUser
}
export const ACTION_HANDLERS = {
  [LIST_USER]: (userData, { payload }) => payload
}
